/**
 * <p>Title: CNPEC 读写cookies</p>
 * <p>Description: IMS</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: CNPEC</p>
 */
package com.mes.cloud.domain.DefaultClient;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 读写cookies类
 *
 * @author yang yi
 * @version 1.0 $Id: SsoAuthentication.java,v 1.0 2009/07/20$
 */
public class SsoAuthentication {
    private static String SSO_IDENTITY = "SSO_IDENTITY";
    private static String SSO_USERID = "SSO_USERID";
    private static String SSO_PROJECTCODE = "SSO_PROJECTCODE";
    private static String SSO_YNDOMAINUSER = "SSO_ISDOMAINUSER";
    private HttpServletRequest request = null;
    private HttpServletResponse response = null;
    private String ssoCookieName = "";
    private String iuserId = "";
    private String identity = "";
    private String ynDomainUser = "";
    private String projectCode = "";
    private String siteID = "";

    public SsoAuthentication() {
    }

    public SsoAuthentication(String siteID) {
        ssoCookieName = "SSO_COOKIES_" + siteID.toUpperCase();
        this.siteID = siteID.toUpperCase();
    }

    public SsoAuthentication(String siteId, HttpServletRequest request, HttpServletResponse response) {
        ssoCookieName = "SSO_COOKIES_" + siteId.toUpperCase();
        this.siteID = siteId.toUpperCase();
        this.request = request;
        this.response = response;
    }

    /**
     * 判断用户是否已登录SSO
     */
    public boolean IsLogin() {

        Cookie cookies[] = request.getCookies();
        if (cookies == null)
            return false;
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals(ssoCookieName))
                return true;
        }
        return false;
    }

    /**
     * 获取用户ID 系统没登录时，用户ID为空字符串
     *
     * @return cookies中的userid
     */
    public String getIuserId() {
        Cookie cookie = getCookie(SSO_USERID);
        if (cookie != null) {
            iuserId = cookie.getValue();
        }
        return iuserId;
    }

    /**
     * 获取用户标识 系统没登录时，用户标识为空字符串
     *
     * @return cookies中的Identity
     */
    public String getIdentity() {
        Cookie cookie = getCookie(SSO_IDENTITY);
        if (cookie != null) {
            identity = cookie.getValue();
        }
        return identity;
    }

    /**
     * 获取域设置用户在系统中的工程代码 系统没登录时，工程代码为空字符串
     *
     * @return cookies中的ProjectCode
     */
    public String getProjectCode() {
        Cookie cookie = getCookie(SSO_PROJECTCODE);
        if (cookie != null) {
            projectCode = cookie.getValue();
        }
        return projectCode;
    }

    /**
     * 设置用户在系统中的工程代码到cookies中
     *
     * @param projectCode 工程代码
     */
    public void setProjectCode(String projectCode) {
        Cookie cookie = getCookie(SSO_PROJECTCODE);
        if (cookie != null) {
            cookie.setValue(projectCode);
            response.addCookie(cookie);
        }
    }

    /**
     * 获取是否域用户登录标识
     * <p>
     * 系统没登录时，用户标识为空字符串
     *
     * @return 用户登录标识
     */
    public String getYnDomainUser() {
        Cookie cookie = getCookie(SSO_YNDOMAINUSER);
        if (cookie != null) {
            ynDomainUser = cookie.getValue();
            if (ynDomainUser != null && !"".equals(ynDomainUser) && ynDomainUser.equals("Y")) {
                return ynDomainUser;
            }
        }
        return "N";
    }

    /// <summary>
    /// 登记身份票据
    /// 保存从认证中心登录成功后返回的用户标识和用户ID
    /// </summary>
    /// <param name="identity">用户标识</param>
    /// <param name="userId">用户ID</param>
    /// <param name="projectCode">工程代码</param>
    /// <param name="ynDomainUser">是否域用户登录</param>

    /**
     * 登记身份票据 保存从认证中心登录成功后返回的用户标识和用户ID
     *
     * @param identity     用户标识
     * @param userId       用户ID
     * @param projectCode  工程代码
     * @param ynDomainUser 是否域用户登录
     */
    public void SetAuthCookie(String identity, String userId, String projectCode, String ynDomainUser) {
        Cookie identityCookies = new Cookie(SSO_IDENTITY, identity);
        identityCookies.setPath("/uehr");
        response.addCookie(identityCookies);

        Cookie userIdCookies = new Cookie(SSO_USERID, userId);
        userIdCookies.setPath("/uehr");
        response.addCookie(userIdCookies);

        Cookie projectCodeCookies = new Cookie(SSO_PROJECTCODE, projectCode);
        projectCodeCookies.setPath("/uehr");
        response.addCookie(projectCodeCookies);

        Cookie ynDomainUserCookies = new Cookie(SSO_YNDOMAINUSER, ynDomainUser);
        ynDomainUserCookies.setPath("/uehr");
        response.addCookie(ynDomainUserCookies);

        Cookie siteIDCookies = new Cookie(ssoCookieName, siteID);
        siteIDCookies.setPath("/uehr");
        response.addCookie(siteIDCookies);

    }

    /**
     * 注消身份票据
     */
    public void SignOut() {
        deleteCookie(ssoCookieName);
    }

    /**
     * 删除cookies
     *
     * @param name 需要删除的cookies的键
     */
    private void deleteCookie(String name) {
        Cookie cookie = getCookie(name);
        if (cookie != null) {
            cookie.setMaxAge(0);
            cookie.setPath("/uehr");
            response.addCookie(cookie);
        }
    }

    /**
     * 获得指定cookies的值
     *
     * @param name 获得指定cookies的键名称
     * @return 返回指定cookies
     */
    private Cookie getCookie(String name) {
        Cookie cookies[] = this.request.getCookies();
        if (cookies == null)
            return null;
        int i = 0;
        for (int len = cookies.length; i < len; i++)
            if (cookies[i].getName().equals(name))
                return cookies[i];

        return null;
    }

}